package com.example.demo.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.domain.Article;
import com.example.demo.domain.User;
import com.example.demo.domain.dto.ArticleDTO;
import com.example.demo.mapper.ArticleMapper;

@Service
public class ArticleServiceImpl implements ArticleService {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	UserService userService;
	@Autowired
	ArticleMapper articleMapper;
	@Autowired
	TagService tagService;
	@Autowired
	ArticleImageService articleImageService;
	@Autowired
	LikeService likeService;
	@Autowired
	CommentService commentService;
	@Autowired
	RepostService repostService;
	@Autowired
	CategoryService categoryService;

	@Override
	public List<ArticleDTO> loadAllArticleDTOByUser(User user) {
		logger.info("----ArticleServiceImpl.loadAllArticleDTOByUser:");
		List<ArticleDTO> allArticleDTO = new ArrayList<ArticleDTO>();
		List<Article> articles = new ArrayList<Article>();
		// 1.获取用户所有文章
		// 2.DTO中加入Article，标签，评论等
		articles = articleMapper.selectArticlesByUid(user.getUid());
		for (Article a : articles) {
			ArticleDTO articleDTO = new ArticleDTO();
			articleDTO.setArticle(a);
			articleDTO.setTags(tagService.getTagsOfArticleByAid(a.getAid()));
			articleDTO.setImages(articleImageService.getImagesOfArticleByAid(a.getAid()));
			articleDTO.setLikes(likeService.getLikesOfArticleByAid(a.getAid()));
			articleDTO.setComments(commentService.getCommentsOfArticleByAid(a.getAid()));
			allArticleDTO.add(articleDTO);
		}
		for (ArticleDTO ato : allArticleDTO) {
			logger.info("----" + ato.getArticle().toString());
			logger.info("----" + ato.getTags().toString());
		}
		return allArticleDTO;
	}

	@Override
	public Integer addArticle(Article article) {

		return articleMapper.insertArticle(article);
	}

	/** 加载全部用户的ADTO **/
	@Override
	public List<ArticleDTO> loadAllArticleDTO() {
		logger.info("----ArticleServiceImpl.loadAllArticleDTO:");
		List<ArticleDTO> allArticleDTO = new ArrayList<ArticleDTO>();
		List<Article> articles = new ArrayList<Article>();
		// 1.获取用户所有文章
		// 2.DTO中加入Article，标签，评论等
		articles = articleMapper.selectArticles();
		for (Article a : articles) {
			ArticleDTO articleDTO = new ArticleDTO();
			articleDTO.setArticle(a);
			articleDTO.setAuthor(userService.getUsernameByUid(a.getUid()));
			articleDTO.setCategory(categoryService.getCategoryNameByCgid(a.getCategory()));
			articleDTO.setRepostCounts(repostService.getRepostsByAid(a.getAid()).size());
			articleDTO.setTags(tagService.getTagsOfArticleByAid(a.getAid()));
			articleDTO.setImages(articleImageService.getImagesOfArticleByAid(a.getAid()));
			articleDTO.setLikes(likeService.getLikesOfArticleByAid(a.getAid()));
			articleDTO.setComentCounts(commentService.getCommentsOfArticleByAid(a.getAid()).size());
			articleDTO.setComments(commentService.getCommentsOfArticleByAid(a.getAid()));
			allArticleDTO.add(articleDTO);
		}
//		for (ArticleDTO ato : allArticleDTO) {
//			logger.info("----" + ato.getArticle().toString());
//			logger.info("----" + ato.getTags().toString());
//		}
		return allArticleDTO;
	}

	@Override
	public ArticleDTO loadArticleDTOByAid(Long aid) {
		if (aid == null) {
			return null;
		}
		ArticleDTO articleDTO = new ArticleDTO();
		Article a = articleMapper.selectArticleByAid(aid);
		articleDTO.setArticle(a);
		articleDTO.setTags(tagService.getTagsOfArticleByAid(a.getAid()));
		articleDTO.setImages(articleImageService.getImagesOfArticleByAid(a.getAid()));
		articleDTO.setLikes(likeService.getLikesOfArticleByAid(a.getAid()));
		articleDTO.setComments(commentService.getCommentsOfArticleByAid(a.getAid()));
		articleDTO.setCategory(categoryService.getCategoryNameByCgid(a.getCategory()));
		articleDTO.setAuthor(userService.getUsernameByUid(a.getUid()));
		return articleDTO;
	}

	@Override
	public List<ArticleDTO> loadAllArticleDTOsByTag(Integer tid) {
		if (tid == null) {
			return null;
		}
		List<Long> allAid = tagService.getAllAidBytid(tid);
		List<ArticleDTO> articleDTOs = new ArrayList<ArticleDTO>();
		logger.info("allAId.size(): "+allAid.size());
		if (allAid.size() > 0) {
			for (Long aid : allAid) {
				ArticleDTO articleDTO = new ArticleDTO();
				articleDTO = loadArticleDTOByAid(aid);
				if(articleDTO!=null) {
					articleDTOs.add(articleDTO);
				}
			}
			return articleDTOs;
		} 
		else {
			return null;
		}
	}
}
